package cn.kgc.tcst004.maoqichao.crowdfundingadminspringboot.controller;

import cn.kgc.tcst004.maoqichao.crowdfundingadminspringboot.common.ResultEntity;
import cn.kgc.tcst004.maoqichao.crowdfundingadminspringboot.pojo.Advertise;
import cn.kgc.tcst004.maoqichao.crowdfundingadminspringboot.service.AdvertiseService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/advertise")
public class AdvertiseController {
    @Resource
    AdvertiseService advertiseService;
    //跳转Advertise页面
    @RequestMapping("/to/page")
    public String page(){
        return "advertise-page";
    }
    //分页显示
    @ResponseBody
    @RequestMapping("/search/by/keyword")
    public ResultEntity<PageInfo<Advertise>> search(@RequestParam(value="pageNum", defaultValue="1",required = false) Integer pageNum,
                                                    @RequestParam(value="pageSize", defaultValue="5",required = false) Integer pageSize,
                                                    @RequestParam(value="keyword", defaultValue="",required = false) String keyword
    ){
        // 1.查询得到PageInfo对象
        PageInfo<Advertise> pageInfo = advertiseService.queryForKeywordAdvertise(pageNum, pageSize, keyword);
        // 2.封装结果对象返回
        return ResultEntity.successWithoutData(pageInfo);
    }
    //数据列表
    @ResponseBody
    @RequestMapping("/get/list/by/id/list")
    public ResultEntity<List<Advertise>> getAdvertiseListByIdList(@RequestBody List<Integer> advertiseIdList){
        List<Advertise> advertiseListByIdList = advertiseService.getAdvertiseListByIdList(advertiseIdList);
        return ResultEntity.successWithoutData(advertiseListByIdList);
    }
    //删除
    @ResponseBody
    @RequestMapping("/batch/remove")
    public ResultEntity<String> batchRemove(@RequestBody List<Integer> advertiseIdList){
        try {
            advertiseService.batchRemove(advertiseIdList);
            return ResultEntity.successWithoutData();
        }catch (Exception e){
            return ResultEntity.failed(null,e.getMessage());
        }
    }
    //添加
    @ResponseBody
    @RequestMapping("/save/advertise")
    public ResultEntity<String> saveAdvertise(Advertise advertise){
        advertiseService.saveAdvertise(advertise);
        return ResultEntity.successWithoutData();
    }
    //修改
    @ResponseBody
    @RequestMapping("/update/advertise")
    public ResultEntity<String> updateAdvertise(Advertise advertise){
        advertiseService.updateAdvertise(advertise);
        return ResultEntity.successWithoutData();
    }
    //跟据id查询修改
    @ResponseBody
    @RequestMapping("/update/select")
    public ResultEntity<Advertise> updateSelect(@RequestParam("id") Integer id){
        Advertise advertise = advertiseService.selectByUpdate(id);
        return ResultEntity.successWithoutData(advertise);
    }
}
